<?php

/**
 * 文章栏目
 */

namespace app\article\common;

class IndexCommon extends \app\base\common\SiteCommon {

    protected function index() {


        $classId = request('get', 'id');

        $pageParams = [];
        $filterWhere = [];
        if ($classId) {
            $classInfo = target('article/ArticleClass')->getInfo($classId);
            if (empty($classInfo)) {
                $this->error404();
            }
            if ($classInfo['url']) {
                $this->redirect($classInfo['url']);
            }
            $this->pageInfo['name'] = $classInfo['name'];
            $this->setMeta($classInfo['name']);

            $crumb = target('article/ArticleClass')->loadCrumbList($classId);
            $this->setCrumb($crumb);

            $pageParams['id'] = $classId;


            $tpl = 'article_' . $this->siteConfig['tpl_class'];
            if ($classInfo['tpl_class']) {
                $tpl = $classInfo['tpl_class'];
            }
            $parentClassInfo = array_slice($crumb, -2, 1);
            if (empty($parentClassInfo)) {
                $parentClassInfo = $crumb[0];
            } else {
                $parentClassInfo = $parentClassInfo[0];
            }
            $topClassInfo = $crumb[0];

            $classIds = target('article/ArticleClass')->getSubClassId($classId);
            if ($classIds) {
                $filterWhere['_sql'] = 'class_id in(' . $classIds . ')';
            }

            $modelId = $classInfo['model_id'];

            $this->assign('classInfo', $classInfo);
            $this->assign('parentClassInfo', $parentClassInfo);
            $this->assign('topClassInfo', $topClassInfo);

        } else {
            $this->pageInfo['name'] = '新闻资讯';
            $this->setMeta('新闻资讯');
            $this->setCrumb([
                [
                    'name' => '新闻资讯',
                    'url' => url()
                ]
            ]);
            $tpl = 'article_index';
            $modelId = 0;
        }

        $where = [];
        $where['A.status'] = 1;

        $contentData = target('article/Article')->where($filterWhere)->field(['content_id'])->select();
        $filter = target('site/SiteFilter')->getFilter($contentData, ['id' => $classId]);
        $attrList = $filter['attrList'];
        if ($filter['ids']) {
            $where['_sql'][] = 'A.content_id in (' . $filter['ids'] . ')';
            $pageLimit = 15;
            $model = target('article/Article');
            $count = $model->countList($where);
            $pageData = $this->pageData($count, $pageLimit, $pageParams);
            $list = $model->loadList($where, $pageData['limit'], '', $modelId);
        } else {
            $list = [];
        }
        $this->assign('attrList', $attrList);
        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->assign('pageParams', $pageParams);
        $this->siteDisplay($tpl);
    }


}